package net.hubt.itc.data;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ScheduleXMLParser extends BasicXMLParser {
	static final String KEY_TEN_SV = "HOTEN";
	static final String KEY_MA_SV = "MSV";// parent node
	static final String KEY_LOP = "LOP";
	static final String KEY_LICH_THI = "LICHTHI";
	static final String KEY_NGAY_THI = "NGAYTHI";
	static final String KEY_GIO_THI = "GIOTHI";
	static final String KEY_PHUT_THI = "PHUTTHI";
	static final String KEY_MON_THI = "MONTHI";
	static final String KEY_PHONG_THI = "PHONGTHI";
	static final String KEY_DIEM_TRU = "DIEMTRU";
	static final String KEY_KIEM_TRA = "KIEMTRA";
	// static final String KEY_SINH_VIEN="SINHVIEN";
	private Element mRootNode;

	public void setRoot(Element root) {
		mRootNode = root;		
	}

	public String getTenSinhVien() {
		return getTagValueLevel1(KEY_TEN_SV, mRootNode);
	}

	public String getMsv() {
		return getTagValueLevel1(KEY_MA_SV, mRootNode);
	}

	public String getLop() {
		return getTagValueLevel1(KEY_LOP, mRootNode);
	}

	public StudentScheduleList parseXmlData() {
		StudentScheduleList scheduleDataList = null;
		if (mRootNode != null) {
			if (mRootNode.getChildNodes().getLength() > 0) {
				// only one student name with a list of transcript data
				scheduleDataList = new StudentScheduleList();

				scheduleDataList.setTenSinhVien(getTenSinhVien());
				scheduleDataList.setMaSv((getMsv()));
				scheduleDataList.setLop((getLop()));

				NodeList nlLichThi = mRootNode
						.getElementsByTagName(KEY_LICH_THI);
				if (nlLichThi.getLength() > 0) {
					// found <MONHOC>
					int cntLichThi = nlLichThi.getLength();
					// extract every single <LICHTHI> element tag
					for (int i = 0; i < cntLichThi; i++) {
						// a single <LICHTHI> element tag
						Node nodeLichThi = nlLichThi.item(i);
						if (nodeLichThi.getNodeType() == Node.ELEMENT_NODE) {
							Element element = (Element) nodeLichThi;

							ScheduleData scheduleDataItem = new ScheduleData();
							scheduleDataItem.setNgayThi(getTagValue(
									KEY_NGAY_THI, element));
							scheduleDataItem.setGioThi(getTagValue(KEY_GIO_THI,
									element));
							scheduleDataItem.setPhutThi(getTagValue(
									KEY_PHUT_THI, element));
							scheduleDataItem.setMonThi(getTagValue(KEY_MON_THI,
									element));
							scheduleDataItem.setPhongThi(getTagValue(
									KEY_PHONG_THI, element));
							scheduleDataItem.setDiemTru(getTagValue(
									KEY_DIEM_TRU, element));
							scheduleDataItem.setKiemTra(getTagValue(
									KEY_KIEM_TRA, element));

							scheduleDataList.add(scheduleDataItem);
						}
					}
				}
			}
		}
		return scheduleDataList;
	}
}
